package com.pedro.streamer.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.pedro.streamer.R;
import com.pedro.streamer.adapter.SoundSourceTabAdapter;
import com.pedro.streamer.manager.MediaPlayerClient;
import com.pedro.streamer.utils.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;


public class SoundSourceView extends ConstraintLayout {


    private AudioControlView audioView;
    private VideoSoundView videoView;
    private RecyclerView recyclerView;
    private TextView closeText;
    private SoundSourceTabAdapter soundSourceTabAdapter;
    private Context mContext;
    public SoundSourceView(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public SoundSourceView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        initView();
    }

    private void initView() {

        videoView = findViewById(R.id.video_manager_layout);
        audioView = findViewById(R.id.audio_control_layout);
        recyclerView = findViewById(R.id.sound_source_recycle);

        closeText = findViewById(R.id.close_text);


        closeText.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                MediaPlayerClient.getInstance(mContext).showVideoView();
            }
        });

        ArrayList  list = new ArrayList<>();
        list.add("视频原声");
        list.add("助播");

        soundSourceTabAdapter = new SoundSourceTabAdapter();
        soundSourceTabAdapter.setClickListener(new SoundSourceTabAdapter.OnTextClickListener() {
            @Override
            public void onTextClick(int position) {
                if (position == 0){
                    videoView.setVisibility(View.VISIBLE);
                    audioView.setVisibility(View.GONE);
                }else {
                    videoView.setVisibility(View.GONE);
                    audioView.setVisibility(View.VISIBLE);
                }

            }
        });
        soundSourceTabAdapter.setList(list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION, 16);//左间距
        recyclerView.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(soundSourceTabAdapter);

    }


    public void syncVideoDate(){
        videoView.syncDate();
    }

}
